﻿using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Drawing;

namespace breakoutsharp.visual
{
    public class DxSprite : IDisposable
    {
        protected Device device;
        protected Texture texture;
        protected Rectangle size;
        protected Color colorKey;
        protected bool disposedValue = false;


        public DxSprite(Device Device, string filepath, string filenameWithoutExtension, Size size)
            : this(Device, filepath, filenameWithoutExtension, size, Color.LimeGreen)
        {

        }

        public DxSprite(Device Device, string filepath, string filenameWithoutExtension, Size s, Color ColorKey)
        {

            string[] files = System.IO.Directory.GetFiles(@"data\" + filepath, filenameWithoutExtension + ".*");

            colorKey = ColorKey;
            size = new Rectangle(new Point(), s);

            //load texture
            try
            {
                texture = TextureLoader.FromFile(Device, files[0], size.Width, size.Height, D3DX.Default, Usage.None, Format.Unknown, Pool.Default, Filter.Point, Filter.Point,
                ColorKey.ToArgb());
            }
            catch (Exception ex)
            {
                throw new System.IO.FileNotFoundException("The file " + filenameWithoutExtension + " was not found in " + filepath, ex);
            }


            device = Device;

            //Using s As Surface = _Texture.GetSurfaceLevel(0)
            // Try
            // Dim desc As SurfaceDescription = s.Description
            // _Size = New Rectangle(0, 0, desc.Width, desc.Height)
            // Catch ex As Exception

            // Finally
            // s.Dispose()
            // End Try
            //End Using
        }


        public Color ColorKey
        {
            get
            {
                return colorKey;
            }
            set
            {
                colorKey = value;
            }
        }

        public Rectangle Size
        {
            get
            {
                return size;
            }
            set
            {
                size = value;
            }
        }

        public void Draw(Sprite s, Rectangle part, int x, int y, SizeF size)
        {
            this.Draw(s, part, new Point(x, y), size);
        }

        public void Draw(Sprite s, Rectangle part, Point Location, SizeF size)
        {
            s.Begin(SpriteFlags.AlphaBlend);
            if (colorKey != Color.LimeGreen)
            {
                device.RenderState.AlphaBlendEnable = true;
                device.RenderState.SourceBlend = Blend.DestinationAlpha;
                device.RenderState.DestinationBlend = Blend.BothInvSourceAlpha;
                s.Draw2D(texture, part, size, new PointF(), 0, Location, Color.White);
                device.RenderState.AlphaBlendEnable = false;
            }
            else
            {
                s.Draw2D(texture, part, size, new PointF(), 0, Location, Color.Wheat);
            }
            s.End();
        }

        // IDisposable
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposedValue)
            {
                if (disposing)
                {
                    // TODO: free other state (managed objects).
                    if ((texture != null)) texture.Dispose();
                    texture = null;
                    device = null;
                }
            }

            // TODO: free your own state (unmanaged objects).
            // TODO: set large fields to null.
            this.disposedValue = true;
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
